<?php
/**
* Thumbnail generation class. <br />
* 
* @author liuxiao<seanliu707@gmial.com>
* @copyright changxiang 2013
* @version 1.0
*/
class Thumbnail{
	private $_width = null;
	private $_height = null;
	private $_src_image = null;
	private $_dst_image = null;
	private $_img_out_func = 'image';
	private $_dst_src = '';
	private $_is_cut = false;
	private static $_instance = null;
	
	public static function getInstance( $cut = false ){
		if ( self::$_instance == null )
			self::$_instance = new self($cut);
		return self::$_instance;
	}
	
	private function __construct( $cut ){
		$this->_is_cut = $cut;
	}
	/**
	 * setting size of the thumbnail.
	 * @param int $width
	 * @param int $height
	 * @param int $new_width
	 * @param int $new_height
	 * @param float $proportion
	 */
	private function setSize( $width , $height , $new_width , $new_height , $propt ){
		if ( $propt != null && 0 < $propt && $propt< 1 ) {
			$this->_width  = $width * $propt;
			$this->_height = $height * $propt;
		} else {
			if ( $new_width	 < 0 || $new_width  >= $width )		$new_width	= $width;
			if ( $new_height < 0 || $new_height >= $height )	$new_height	= $height;
				
			if ( $new_width != null && $new_height == null ) {
				$this->_width  = $new_width;
				$this->_height = $new_width/$width * $height;
			} elseif ( $new_width == null && $new_height != null ) {
				$this->_width  = $new_height/height * $width;
				$this->_height = $new_width;
			} elseif ( $new_width != null && $new_height != null ) {
				$this->_width  = $new_width;
				$this->_height = $new_height;
			} else {
				$this->_width  = $width;
				$this->_height = $height;
			}
		}
	}
	/**
	 * Initialize images.
	 * @param string $src_img
	 * @param string $dst_img
	 */
	private function imgInit($src_img,$dst_img){
		$_img_func = 'imagecreatefrom';
		$_img_info = pathinfo($src_img);
		
		$_src_type = strtolower($_img_info['extension']);
		if ( $_src_type == 'jpg' ) $_img_func .= 'jpeg';
		else $_img_func .= $_src_type;
		
		$dst_src = '';
		if ( $dst_img == null ) {
			$dst_src = $_img_info['dirname'].'/'.'thumb_'.$_img_info['basename'];
			$this->_img_out_func .= $_src_type=='jpg' ? 'jpeg' : $_src_type;
		} else{
			if ( strpos($dst_img,'../') !== false ){
				$dst_img = str_replace('../', '', $dst_img);
				$_img_info['dirname'] = dirname($_img_info['dirname']);
			}
			
			if ( strrchr($dst_img, '.') !=false ) {
				$dst_src = $_img_info['dirname'].'/'.$dst_img;
				$_dst_type = pathinfo($dst_img,PATHINFO_EXTENSION);
				$this->_img_out_func .= $_dst_type =='jpg' ? 'jpeg' : $_dst_type;
			} else {
				$dst_src = $_img_info['dirname'].'/'.$dst_img.'/thumb_'.$_img_info['basename'];
				$this->_img_out_func .= $_src_type=='jpg' ? 'jpeg' : $_src_type;
			}
		}
		$this->_dst_src = $dst_src;
		
		$_dir = '';
		$_names = explode('/', dirname($dst_src));
		foreach ( $_names as $value ){
			$_dir .= $value.'/';
			if ( ! file_exists($_dir) ) @mkdir($_dir,0777);
		}
		$this->_src_image = $_img_func( $src_img );
		$this->_dst_image = imagecreatetruecolor($this->_width, $this->_height);
		$_color = imagecolorallocate($this->_dst_image, 255, 255, 255);
		imagefill($this->_dst_image, 0, 0, $_color);
	}
	
	/**
	 * create the thumbnail and put out.
	 * @param string $src_img
	 * @param string $dst_img
	 * @param mixed $proportion	比例（$proportion）和宽高任给一组值，比例优先
	 * @param mixed $max_width	宽高可以任给一个值（另一个自动缩放）或两个值都给定
	 * @param mixed $max_height
	 */
	public function creatThumb( $src_img, $dst_img=null, $proportion=null, $max_width=null, $max_height=null ) {
		if ( ! file_exists($src_img) )
			//exit('The source graph is not exist!');
			return false;
		
		list( $_src_width, $_src_height, $_src_type ) = getimagesize( $src_img );
		
		$this->setSize($_src_width, $_src_height, $max_width, $max_height, $proportion);
		
		$this->imgInit($src_img, $dst_img);
		
		if ( $this->_is_cut == true ){
			if ( function_exists('imagecopyresampled') )
				imagecopyresampled($this->_dst_image, $this->_src_image, 0, 0, 0,0, $this->_width, $this->_height, $this->_width, $this->_height);
			else 
				imagecopyresized($this->_dst_image, $this->_src_image, 0, 0, 0, 0, $this->_width, $this->_height, $this->_width, $this->_height);
		} else {
			if ( function_exists('imagecopyresampled') )
				imagecopyresampled($this->_dst_image, $this->_src_image, 0, 0, 0,0, $this->_width, $this->_height, $_src_width, $_src_height);
			else
				imagecopyresized($this->_dst_image, $this->_src_image, 0, 0, 0, 0, $this->_width, $this->_height, $_src_width, $_src_height);
		}
		
		$_out_func = $this->_img_out_func;
		if ( ! $_out_func($this->_dst_image,$this->_dst_src) )	return false;
		
		imagedestroy($this->_dst_image);
		return basename($this->_dst_src);
	}
	
	public function getThumbPath(){
		return $this->_dst_src;
	}
}